﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.ThingsWeb.BuyerInfo
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session[ConstantString.SessionCurrentBuyerAccount] == null)
                {
                    Session[ConstantString.SessionSourceURL] = Context.Request.Url;
                    Response.Redirect(ConstantString.LoginPageURL);
                }
                else
                {
                    Things.Model.Buyer buyer = Session[ConstantString.SessionCurrentBuyerAccount] as Things.Model.Buyer;
                    ShowInfo(buyer);
                }
            }
        }

        private void ShowInfo(Things.Model.Buyer buyer)
        {
            if (buyer != null)
            {
                Things.BLL.BuyerInfo bll = new Things.BLL.BuyerInfo();

                Things.Model.BuyerInfo model = bll.GetModel(buyer.Account);
                if (model != null)
                {
                    this.txtPetName.Text = model.PetName;
                    this.txtRealName.Text = model.RealName;
                    this.txtEmail.Text = model.Email;
                    this.txtMobilePhone.Text = model.MobilePhone;
                    this.txtAddress.Text = model.Address;

                    if (model.BuyerIamge != null)
                    {
                        Session[ConstantString.SessionImages] = model.BuyerIamge;
                        this.Image1.ImageUrl = "~/DisplayImage.aspx";
                    }
                    
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            Things.Model.Buyer buyer = Session[ConstantString.SessionCurrentBuyerAccount] as Things.Model.Buyer;
            if (buyer == null)
            {
                strErr += "帐号信息有误！\\n";
            }

            byte[] content = Things.Common.UICommon.GetImages(FileUpload1);

            if (string.IsNullOrEmpty(FileUpload1.FileName) == false && content.Length <= 0)
            {
                strErr += "图片格式不正确！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }

            string PetName = this.txtPetName.Text;
            string RealName = this.txtRealName.Text;
            string Email = this.txtEmail.Text;
            string MobilePhone = this.txtMobilePhone.Text;
            string Address = this.txtAddress.Text;

            Things.Model.BuyerInfo model = new Things.Model.BuyerInfo();
            model.BuyerAccount = buyer.Account;
            model.PetName = PetName;
            model.RealName = RealName;            
            model.Email = Email;
            model.MobilePhone = MobilePhone;
            model.Address = Address;
            if (content.Length > 0)
            {
                model.BuyerIamge = content;
            }
            else
            {
                model.BuyerIamge = null;
            }

            Things.BLL.BuyerInfo bll = new Things.BLL.BuyerInfo();

            Things.Model.BuyerInfo buyerInfo = bll.GetModel(buyer.Account);

            if (buyerInfo!=null)
            {
                if (content.Length > 0)
                {
                    model.BuyerIamge = content;
                }
                else
                {
                    model.BuyerIamge = buyerInfo.BuyerIamge;
                }
                bll.Update(model);
            }
            else
            {
                bll.Add(model);
            }
            Things.Common.MessageBox.Show(this, "保存成功！");

            if (model.BuyerIamge!=null && model.BuyerIamge.Length > 0)
            {

                Session[ConstantString.SessionImages] = model.BuyerIamge;
                this.Image1.ImageUrl = "~/DisplayImage.aspx";
                this.Image1.Visible = true;
            }
            else
            {
                this.Image1.Visible = false;
            }
        }

    }
}
